package com.darcye.clear.utils;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

public class DeleteLogger {

	private final static String LOG_FILE_NAME = "delete.log";
	
	private static File mLogFile;
	
	public static void initLogger(String logPath){
		String filePath = "";
		if(logPath.endsWith("/")||logPath.endsWith("\\")){
			filePath = logPath + LOG_FILE_NAME;
		}else{
			filePath = logPath + File.separator + LOG_FILE_NAME;
		}
		mLogFile = new File(filePath);
		if(mLogFile.exists()){
			mLogFile.delete();
		}else{
			File p = new File(logPath);
			if(!p.exists()){
				p.mkdirs();
			}
		}
	}
	
	private BufferedWriter mWriter;
	
	public DeleteLogger(String type){
		try {
			mWriter = new BufferedWriter(new FileWriter(mLogFile,true));
			mWriter.write("Delete Resource Type: "+type);
			mWriter.newLine();
			mWriter.write("==============================================================");
			mWriter.newLine();
		} catch (IOException e) {
			e.printStackTrace();
			finish();
		}
	}
	
	public void log(String resName){
		log(resName, null);
	}
	
	public void log(String resName, String resPath){
		try {
			mWriter.write(getFormatLog(resName,resPath));
			mWriter.newLine();
		} catch (IOException e) {
			e.printStackTrace();
			finish();
		}
	}
	
	public void finish(){
		if(mWriter !=null){
			try {
				mWriter.newLine();
				mWriter.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
	private String getFormatLog(String resName,String resPath){
		StringBuilder sb = new StringBuilder();
		sb.append(resName);
		sb.append("      ");
		if(resPath !=null)
			sb.append(resPath);
		return sb.toString();
	}
}
